import redis.asyncio as redis
from redis.exceptions import ConnectionError, TimeoutError

from config import config

redis_pool = redis.ConnectionPool(
    host=config.REDIS_POOL_HOST,
    port=config.REDIS_POOL_PORT,
    # password=config.REDIS_POOL_PASSWORD,
    decode_responses=config.REDIS_POOL_RESPONSES,
    encoding="utf-8"
)


async def get_redis_client() -> redis.Redis:
    try:
        redis_client = redis.Redis(connection_pool=redis_pool)
        sig = await redis_client.ping()
        print("redis_client.ping:", sig)
        return redis_client
    except ConnectionError:
        print("Redis Connect Error")
    except TimeoutError:
        print("Redis Timeout Error")
    except Exception as e:
        print(e)
